﻿@using AntDesign.TableModels
@using System.ComponentModel.DataAnnotations

<Table TItem="TestData" DataSource="@testData">
    <Column @bind-Field="@context.Id" Sortable/>
    <Column Title="DayOfWeek" @bind-Field="@context.DayOfWeek" Sortable/>
    <Column Title="DayOfWeek DataIndex" TData="int" DataIndex="DayOfWeek" Sortable SorterCompare="@((v1, v2) => v1 - v2)" Filters="DayOfWeekFilter" OnFilter="(a, b) => a == b"/>
    <Column Title="N1.N12.N2A DataIndex" TData="string" DataIndex=@("N1.N12['test'].N2A") Sortable/>
    <Column Title="N1.N1A DataIndex Nullable" TData="int?" DataIndex="N1.N1A" Sortable SorterCompare="CompareNullableInt"/>
    <Column Title="Time" TData="DateTime?" DataIndex=@(@"N1.N12['test'].Time") Format="yyyy-MM-dd hh:mm:ss" Sortable SorterCompare="_dateTimeCompare"></Column>
</Table>

@code {

    TestData[] testData;

    public class TestData
    {
        public int Id { get; set; }

        public int DayOfWeek { get; set; }

        public int Jobs { get; set; }

        public int Popularity { get; set; }

        public Nested1 N1 { get; set; }
    }

    public class Nested1
    {
        public int? N1A { get; set; }

        public Dictionary<string, Nested2> N12 { get; set; }
    }

    public class Nested2
    {
        public string N2A { get; set; }

        [Display(Name = "myTime")]
        public DateTime? Time { get; set; }
    }

    public TableFilter<int>[] DayOfWeekFilter =
    {
        new() {Text = "Monday", Value = 1},
        new() {Text = "Tuesday", Value = 2},
        new() {Text = "Wednesdays", Value = 3},
        new() {Text = "Thursday", Value = 4},
        new() {Text = "Friday", Value = 5},
        new() {Text = "Saturday", Value = 6},
        new() {Text = "Sunday", Value = 7},
    };

    public string[] DayName = {"None", "Monday", "Tuesday", "Wednesdays", "Thursday", "Friday", "Saturday", "Sunday"};

    Func<DateTime?, DateTime?, int> _dateTimeCompare = (t1, t2) =>
    {
        if (t1.HasValue && t2.HasValue)
            return DateTime.Compare(t1.Value, t2.Value);
        if (t1.HasValue)
            return 1;
        if (t2.HasValue)
            return -1;

        return 0;
    };

    /// <inheritdoc />
    protected override void OnInitialized()
    {
        testData = GetForecastAsync();
        base.OnInitialized();
    }

    public TestData[] GetForecastAsync()
    {
        var rng = new Random(42);
        var startDate = new DateTime(DateTime.Today.Year - 5, 1, 1);
        var range = (DateTime.Today - startDate).TotalSeconds;
        return Enumerable.Range(0, 5).Select(index =>
        {
            var data = new TestData
            {
                Id = index,
                DayOfWeek = rng.Next(1, 8),
                Jobs = rng.Next(0, 20),
                Popularity = rng.Next(0, 30),
            };
            data.N1 = new()
            {
                N1A = (index & 1) == 0 ? index : null,
                N12 = new()
                {
                    {"no used", null},
                    {
                        "test", new Nested2()
                        {
                            N2A = DayName[data.DayOfWeek] + " - DI",
                            Time = startDate.AddSeconds(rng.Next((int)range))
                        }
                    }
                }
            };
            return data;
        }).ToArray();
    }

    public int CompareNullableInt(int? a, int? b)
    {
        if (a is null && b is null)
        {
            return 0;
        }
        if (a is null)
        {
            return -1;
        }
        if (b is null)
        {
            return 1;
        }
        return (a - b).Value;
    }

}